//
//  SingleView.m
//  Project two framework
//
//  Created by rimi on 16/7/27.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "SingleView.h"
#import "ZZConfig.h"
#import "LUtilityTools.h"
#import "ListViewController.h"
#import "ZPZNetRequest+getClientList.h"
@interface SingleView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *shopingImage;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIButton *shopingBtn;
@property (nonatomic, strong) UILabel *millon;
@end

@implementation SingleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self interface];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)interface
{
    [self addSubview:self.shopingImage];
    [self.shopingImage addSubview:self.shopingBtn];
    [self.shopingImage addSubview:self.closeBtn];
    [self.shopingImage addSubview:self.shopingTextField];
    [self.shopingImage addSubview:self.millon];
}

+ (instancetype)shareValue
{
    static SingleView *shareValue =nil;
    if (shareValue == nil) {
        shareValue = [[SingleView alloc]init];
    }
    return shareValue;
}

+ (void)showOtherView
{
    static SingleView *otherView = nil;
    UIWindow *kewWindow = [[UIApplication sharedApplication] keyWindow];
    if (!otherView) {
        otherView = [[SingleView alloc] initWithFrame:kewWindow.bounds];
        otherView.center = CGPointMake(kewWindow.center.x, kewWindow.center.y * 3);
    }
    [kewWindow addSubview:otherView];
    
    [UIView animateWithDuration:1 animations:^{
        otherView.center = kewWindow.center;
        } completion:^(BOOL finished) {
    }];
}
#pragma mark - 按钮的实现方法
- (void)closeBtnAction
{
    [UIView animateWithDuration:1 animations:^{
        self.center = CGPointMake(self.center.x, 3 * self.center.y);
        self.shopingTextField.text = nil;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

- (void)shopingBtnAction
{
    if ([_shopingTextField.text isEqualToString:@""]) {
        [LUtilityTools showAlertWithTitle:@"提示" message:@"请输入正确购买量" otherTitle:@"确定"];
    } else {

        NSDictionary *dic = @{@"DetailId":[SingleView shareValue].ID};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shopingAction" object:nil userInfo:dic];
        [self closeBtnAction];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL isEditing = YES;
    if ([textField.text rangeOfString:@"."].location == NSNotFound) {
        isEditing = NO;
    }
    if ([string length] > 0) {
        /** 当前输的的字符 */
        unichar single = [string characterAtIndex:0];
        /** 正确的输入格式 */
        if ((single >= '0' && single <= '9') || single == '.') {
            if ([textField.text length] == 0) {
                if (single == '.') {
                    /** 第一个数字不能为小数点 */
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
                /** 第一个数字不能为0 */
                if (single == '0') {
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            if (single == '.') {
                /** 如果text中还没有小数点 */
                if (!isEditing) {
                    isEditing = YES;
                    return YES;
                }else{
                    /** 不能再次输入小数点 */
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            else {
                if (isEditing) {
                    NSRange ran = [textField.text rangeOfString:@"."];
                    NSInteger number = range.location - ran.location;
                    if (number <= 2) {
                        return YES;
                    }else {
                        /** 只能输入两位小数 */
                        return NO;
                    }
                }
                else{
                    return YES;
                }
            }
        }else {
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }else {
        return YES;
    }
}


- (UIImageView *)shopingImage
{
    if (_shopingImage == nil) {
        _shopingImage = ({
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.bounds = CGRectMake(0, 0, 420, 300);
            imageView.center = self.center;
            imageView.image = [UIImage imageNamed:@"购买量_05.png"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });
    }
    return _shopingImage;
}

- (UIButton *)closeBtn
{
    if (_closeBtn == nil) {
        _closeBtn = ({
            UIButton *btn = [UIButton buttonWithType: UIButtonTypeSystem];
            btn.frame = CGRectMake(CGRectGetMaxX(self.shopingImage.bounds) - 55, 15, 40, 40);
            [btn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _closeBtn;
}
- (UIButton *)shopingBtn
{
    if (_shopingBtn == nil) {
        _shopingBtn = ({
            UIButton *btn = [UIButton buttonWithType: UIButtonTypeSystem];
            btn.frame = CGRectMake(135, 213, 150, 40);
            [btn addTarget:self action:@selector(shopingBtnAction) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _shopingBtn;
}

- (UITextField *)shopingTextField
{
    if (_shopingTextField == nil) {
        _shopingTextField = ({
            UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(65, 125, 250, 40)];
            textField.delegate = self;
            textField.textAlignment = NSTextAlignmentRight;
            textField;
        });
    }
    return _shopingTextField;
}
- (UILabel *)millon
{
    if (_millon == nil) {
        _millon = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.shopingTextField.frame), CGRectGetMinY(self.shopingTextField.frame), 50, 40)];
            label.text = @"万元";
            label.font = [UIFont systemFontOfSize:18];
            label;
        });
    }
    return _millon;
}

@end
